
#include <Inklude/ikCDataPacker.h>

using namespace Inklude;

Inklude::ikCDataPacker g_dataPacker;

bool ikCDataPacker::CheckSize(int DataSize, int BufferSize)
{
    return BufferSize >= DataSize;
}


template <>
int ikCDataPacker::PackData<std::string>(char* Buffer, int BufferSize, std::string Data)
{
    int _strSize = static_cast<int>(Data.length() + 1);

    if (CheckSize(_strSize, BufferSize))
    {
        memcpy_s(Buffer, BufferSize, Data.c_str(), _strSize);
    }
    return _strSize;
}

template<>
int ikCDataPacker::UnpackData<std::string>(char* Buffer, int BufferSize, std::string* Data)
{
    int _strSize = static_cast<int>(strlen(Buffer) + 1);
    
    if (CheckSize(_strSize, BufferSize))
    {
        Data->assign(Buffer);
    }
    return _strSize;
}

template<>
int ikCDataPacker::PackData<std::wstring>(char* Buffer, int BufferSize, std::wstring Data)
{
    int _strSize = (static_cast<int>(Data.length() + 1)) * 2;

    if (CheckSize(_strSize, BufferSize))
    {
        memcpy_s(Buffer, BufferSize, Data.c_str(), _strSize);
    }
    return _strSize;
}

template<>
int ikCDataPacker::UnpackData<std::wstring>(char* Buffer, int BufferSize, std::wstring* Data)
{
    wchar_t* _buf = reinterpret_cast<wchar_t*>(Buffer);

    int _strSize = (static_cast<int>(wcslen(_buf) + 1)) * 2;
    
    if (CheckSize(_strSize, BufferSize))
    {
        Data->assign(_buf);
    }
    return _strSize;
}

